//@charset "utf-8";
//选股数据
define(function (require, exports, module) {
	var Backbone = require('backbone');
	var relationQueryModel = require('../models/relationquery');

	var RelationQueryView = Backbone.View.extend({
		el: '#view_search_options_relation',
		initialize: function() {
			//
		},
		events : {
			'click .view_relation_query_item' : 'changeSearchInput',
			'mouseenter .view_relation_query_item' : 'addActiveClass'
		},
		request : function (query) {
			var self = this;
			relationQueryModel.getRelationQuery(query, function () {
				var relationQueryList = relationQueryModel.get('result');
				self.render(relationQueryList);
			});
		},
		render : function (relationQueryList) {
			if (relationQueryList.length <= 0) {
				this.$el.hide();

				return ;
			}

			var html = [];
			var index = 1;
			for (var i in relationQueryList) {
				var relationQueryItem = relationQueryList[i];
				html.push('<a href="###" view_click_stat="cop_58120ab6_458" class="search-option view_relation_query_item">'+relationQueryItem+'</a>');

				if (index++ >= 3) {
					break;
				}
			}

			this.$el.html(html.join(''));
			this.$el.show();
		},
		changeSearchInput : function (e) {
			e = e || window.event;
			var str = $(e.currentTarget).text();
			$('#view_searchinput').val(str);
			this.$el.hide();

			$('#view_searchbtn').click();
		},
		addActiveClass : function (e) {
			e = e || window.event;
			if ($(e.currentTarget).hasClass('active')) {
				return ;
			}

			$('.active.view_relation_query_item').removeClass('active');
			$(e.currentTarget).addClass('active');
		},
		hideEl : function () {
			this.$el.hide();
		}
	});

	module.exports = new RelationQueryView();
});
